package cn.lingyangwl.agile.pay.mch.controller;

import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.agile.pay.model.entity.PayInterfaceConfig;
import cn.lingyangwl.agile.pay.model.rqrs.PayConfigQL;
import cn.lingyangwl.agile.pay.model.rqrs.PayInterfaceConfigRQ;
import cn.lingyangwl.agile.pay.model.rqrs.PayInterfaceConfigRS;
import cn.lingyangwl.agile.pay.model.rqrs.PayInterfaceRQ;
import cn.lingyangwl.agile.pay.common.constants.Cs;
import cn.lingyangwl.agile.pay.common.service.PayInterfaceConfigService;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.framework.security.xss.core.XssCleanIgnore;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商户支付接口配置类
 *
 * @author shenguangyang
 */
@RestController
@RequestMapping("/mch/payConfig")
public class MchPayInterfaceConfigController {

    @Resource
    private PayInterfaceConfigService payInterfaceConfigService;

    /**
     * 查询商户支付接口配置列表
     *
     * @return shenguangyang
     */
    @RamResource(action = "pay:interfaceConfig:list", name = "查询商户支付接口配置列表", type = ActionType.READ)
    @GetMapping("/list")
    public Resp<List<PayInterfaceConfigRS>> list(@Validated PayConfigQL query) {
        List<PayInterfaceConfigRS> list = payInterfaceConfigService.listPayIfConfig(query);
        return Resp.ok(list);
    }


    /**
     * 根据 商户号、接口类型 获取商户参数配置
     */
    @RamResource(action = "pay:interfaceConfig:get", name = "查询商户支付接口配置详情", type = ActionType.READ)
    @GetMapping("/getPayConfig")
    public Resp<PayInterfaceConfig> getByMchNo(@RequestParam(value = "appId") String appId,
                                               @RequestParam(value = "ifCode") String ifCode) {
        PayInterfaceConfig payInterfaceConfig = payInterfaceConfigService.getByInfoIdAndIfCode(Cs.INFO_TYPE_MCH_APP, appId, ifCode);
        return Resp.ok(payInterfaceConfig);
    }

    /**
     * 更新商户支付参数
     */
    @PostMapping("/update")
    @RamResource(action = "pay:interfaceConfig:update", name = "更新商户支付接口配置", type = ActionType.WRITE)
    @XssCleanIgnore({"ifParams"})
    public Resp<?> update(@RequestBody PayInterfaceConfigRQ req) {
        payInterfaceConfigService.updatePayIfConfig(req);
        return Resp.ok();
    }

    /**
     * 添加支付接口
     */
    @PostMapping("/addPayIf")
    @RamResource(action = "pay:interfaceConfig:addPayIf", name = "添加支付接口", type = ActionType.WRITE)
    public Resp<?> addPayIf(@RequestBody PayInterfaceRQ req) {
        payInterfaceConfigService.addPayIf(req);
        return Resp.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @RamResource(action = "pay:interfaceConfig:delete", name = "删除支付配置", type = ActionType.WRITE)
    public Resp<?> delete(@RequestParam("id") String id) {
        payInterfaceConfigService.deletePayIfConfig(id);
        return Resp.ok();
    }


//    /** 查询支付宝商户授权URL **/
//    @GetMapping("/alipayIsvsubMchAuthUrls/{mchAppId}")
//    public ApiRes queryAlipayIsvsubMchAuthUrl(@PathVariable String mchAppId) {
//
//        MchApp mchApp = mchAppService.getById(mchAppId);
//
//        if (mchApp == null || !mchApp.getMchNo().equals(getCurrentMchNo())) {
//            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_SELETE);
//        }
//
//        MchInfo mchInfo = mchInfoService.getById(mchApp.getMchNo());
//        DBApplicationConfig dbApplicationConfig = sysConfigService.getDBApplicationConfig();
//        String authUrl = dbApplicationConfig.genAlipayIsvsubMchAuthUrl(mchInfo.getIsvNo(), mchAppId);
//        String authQrImgUrl = dbApplicationConfig.genScanImgUrl(authUrl);
//
//        JSONObject result = new JSONObject();
//        result.put("authUrl", authUrl);
//        result.put("authQrImgUrl", authQrImgUrl);
//        return ApiRes.ok(result);
//    }
//
//
//    /** 查询当前应用支持的支付接口 */
//    @PreAuthorize("hasAuthority( 'ENT_DIVISION_RECEIVER_ADD' )")
//    @RequestMapping(value="ifCodes/{appId}", method = RequestMethod.GET)
//    public ApiRes getIfCodeByAppId(@PathVariable("appId") String appId) {
//
//        if(mchAppService.count(MchApp.gw().eq(MchApp::getMchNo, getCurrentMchNo()).eq(MchApp::getAppId, appId)) <= 0){
//            throw new BizException("商户应用不存在");
//        }
//
//        Set<String> result = new HashSet<>();
//
//        payInterfaceConfigService.list(PayInterfaceConfig.lqw().select(PayInterfaceConfig::getIfCode)
//        .eq(PayInterfaceConfig::getState, CS.PUB_USABLE)
//        .eq(PayInterfaceConfig::getInfoId, appId)
//        .eq(PayInterfaceConfig::getInfoType, CS.INFO_TYPE_MCH_APP)
//        ).stream().forEach(r -> result.add(r.getIfCode()));
//
//        return ApiRes.ok(result);
//    }


}
